package net.lab1024.sa.user.module.business.course.service;

import java.util.List;
import java.util.stream.Collectors;

import net.lab1024.sa.common.common.util.SmartRequestUtil;
import net.lab1024.sa.user.module.business.course.constant.CourseOrderStatus;
import net.lab1024.sa.user.module.business.course.dao.CourseDao;
import net.lab1024.sa.user.module.business.course.dao.CourseOrderDao;
import net.lab1024.sa.user.module.business.course.domain.entity.CourseEntity;
import net.lab1024.sa.user.module.business.course.domain.entity.CourseOrderEntity;
import net.lab1024.sa.user.module.business.course.domain.form.*;
import net.lab1024.sa.user.module.business.course.domain.vo.CourseOrderVO;
import net.lab1024.sa.common.common.util.SmartBeanUtil;
import net.lab1024.sa.common.common.util.SmartPageUtil;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.user.module.business.course.domain.vo.CourseVO;
import net.lab1024.sa.user.module.business.course.domain.vo.StudentVO;
import net.lab1024.sa.user.module.business.points.constant.PointsChangeStatus;
import net.lab1024.sa.user.module.business.points.constant.PointsChangeType;
import net.lab1024.sa.user.module.business.points.domain.form.PointsChangeRecordAddForm;
import net.lab1024.sa.user.module.business.points.domain.vo.PointsBalanceVO;
import net.lab1024.sa.user.module.business.points.service.PointsChangeRecordService;
import net.lab1024.sa.user.module.business.points.service.PointsService;
import org.apache.commons.collections4.CollectionUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 课程订单 Service
 *
 * @Author jabari
 * @Date 2023-08-05 17:49:48
 * @Copyright 51dev.cn
 */

@Service
public class CourseOrderService {
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private CourseOrderDao courseOrderDao;

    @Autowired
    private PointsService pointsService;

    @Autowired
    private PointsChangeRecordService pointsChangeRecordService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<CourseOrderVO> queryPage(CourseOrderQueryForm queryForm) {
        //学生ID集合
        StudentQueryForm studentQueryForm=new StudentQueryForm();
        studentQueryForm.setUserId(SmartRequestUtil.getRequestUserId());
        studentQueryForm.setPageNum(1);
        studentQueryForm.setPageSize(50);
        PageResult<StudentVO> studentVOPageResult = studentService.queryPage(studentQueryForm);
        queryForm.setStudentIds(studentVOPageResult.getList().stream().map(StudentVO::getStudentId).collect(Collectors.toList()));
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CourseOrderVO> list = courseOrderDao.queryPage(page, queryForm);
        list.forEach(courseOrderVO -> {
            CourseQueryForm courseQueryForm=new CourseQueryForm();
            courseQueryForm.setCourseId(courseOrderVO.getCourseId());
            courseQueryForm.setPageNum(1);
            courseQueryForm.setPageSize(10);
            PageResult<CourseVO> courseVOPageResult = courseService.queryPage(courseQueryForm);
            if(!courseVOPageResult.getEmptyFlag()){
                courseOrderVO.setCourseInfo(courseVOPageResult.getList().get(0));
            }


        });
        PageResult<CourseOrderVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(CourseOrderAddForm addForm) {
        CourseOrderEntity courseOrderEntity = SmartBeanUtil.copy(addForm, CourseOrderEntity.class);
        //查找课程对应的积分价格
        CourseEntity courseEntity = courseDao.selectById(addForm.getCourseId());
        if(courseEntity == null){
            return ResponseDTO.userErrorParam("课程无效");
        }
        //查看积分余额
        ResponseDTO<PointsBalanceVO> pointsBalanceRsp = pointsService.getPointsBalance();
        if(!pointsBalanceRsp.getOk()){
            return ResponseDTO.userErrorParam(pointsBalanceRsp.getMsg());
        }
        if(pointsBalanceRsp.getData().getPoints()<courseEntity.getPointsPrice()){
            return ResponseDTO.userErrorParam("积分余额不足");
        }


        courseOrderEntity.setPayPoints(courseEntity.getPointsPrice());
        courseOrderEntity.setOrderStatus(CourseOrderStatus.COMPLETED.getValue());

        //添加课程报名订单
        courseOrderDao.insert(courseOrderEntity);

        //添加积分扣减记录
        PointsChangeRecordAddForm pointsChangeRecordAddForm=new PointsChangeRecordAddForm();
        pointsChangeRecordAddForm.setCourseId(addForm.getCourseId());
        pointsChangeRecordAddForm.setChangePoints(courseEntity.getPointsPrice());
        pointsChangeRecordAddForm.setChangeStatus(PointsChangeStatus.COMPLETED.getValue());
        pointsChangeRecordAddForm.setChangeType(PointsChangeType.REDUCE.getValue());
        pointsChangeRecordAddForm.setOrderId(courseOrderEntity.getOrderId());
        pointsChangeRecordAddForm.setRemark("积分换课("+courseEntity.getCourseName()+")");
        pointsChangeRecordAddForm.setUserId(SmartRequestUtil.getRequestUserId());

        pointsChangeRecordService.add(pointsChangeRecordAddForm);

        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(CourseOrderUpdateForm updateForm) {
        CourseOrderEntity courseOrderEntity = SmartBeanUtil.copy(updateForm, CourseOrderEntity.class);
        courseOrderDao.updateById(courseOrderEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        courseOrderDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer orderId) {
        if (null == orderId){
            return ResponseDTO.ok();
        }

        courseOrderDao.deleteById(orderId);
        return ResponseDTO.ok();
    }
}
